/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utility;

import java.io.IOException;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.SortedMapWritable;
import org.apache.hadoop.io.Writable;

/**
 *
 * @author takeshi
 */
//MapWritable
public class TupleEx extends SortedMapWritable {
    //Float, Float, Integer

    private Float first;
    private Float second;
    private Integer third;

    public TupleEx() {
        first = 0f;
        second = 0f;
        third = 0;
        put(new IntWritable(1), new FloatWritable(first));
        put(new IntWritable(2), new FloatWritable(second));
        put(new IntWritable(3), new IntWritable(third));
    }
    
    public TupleEx(float fst, float snd, int trd) {
        clear();
        first = fst;
        second = snd;
        third = trd;
        put(new IntWritable(1), new FloatWritable(first));
        put(new IntWritable(2), new FloatWritable(second));
        put(new IntWritable(3), new IntWritable(third));
    }

    public TupleEx(TupleEx other) {
        clear();
        first = other.first;
        second = other.second;
        third = other.third;
        put(new IntWritable(1), new FloatWritable(first));
        put(new IntWritable(2), new FloatWritable(second));
        put(new IntWritable(3), new IntWritable(third));
    }

    public TupleEx Sum(TupleEx v1, TupleEx v2) {
        if (v1 == null) {
            return v2;
        }
        if (v2 == null) {
            return v1;
        }
        TupleEx rst = new TupleEx();
        rst.put(new IntWritable(1), new FloatWritable(first));
        put(new IntWritable(2), new FloatWritable(second));
        put(new IntWritable(3), new IntWritable(third));
        return rst;
    }

    public Writable Get(int id) {
        if (containsKey(new IntWritable(id))) {
            return get(new IntWritable(id));
        } else {
            return null;
        }
    }
    
    public void Put(int id, Writable val) throws IOException {
        if(id >2 || val == null) throw new IOException("Tuple id can not larger than 2 and val ="+val +"\n");
        put(new IntWritable(id), val);
        if(id == 0) first = ((FloatWritable)val).get();
        if(id == 1) second = ((FloatWritable)val).get();
        if(id == 2) third = ((IntWritable)val).get();
    }

    @Override
    public String toString() {
        return first+ "\t " + second + "\t " + third;
    }

    public Float GetFirst() {
        return first;
    }

    public Float GetSecond() {
        return second;
    }

    public Integer GetThird() {
        return third;
    }
}
